package com.yyplatform.admin.platform.role.service.impl;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.role.model.dto.SysCaptchaLogDTO;
import com.yyplatform.admin.platform.role.model.dto.SysCaptchaLogQueryDTO;
import com.yyplatform.admin.platform.role.model.entity.SysCaptchaLogEntity;
import com.yyplatform.admin.platform.role.repository.SysCaptchaLogRepository;
import com.yyplatform.admin.platform.role.service.SysCaptchaLogService;
import com.yyplatform.admin.infra.utils.DateUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/24 23:15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysCaptchaLogServiceImpl implements SysCaptchaLogService {

    private SysCaptchaLogRepository sysCaptchaLogRepository;

    private ModelMapper modelMapper;

    @Override
    public Page<SysCaptchaLogDTO> query(SysCaptchaLogQueryDTO queryDTO) {
        Page<SysCaptchaLogEntity> entityPage = sysCaptchaLogRepository.findPage(
                DateUtils.getBeginDate(queryDTO.getCreateTimeArr()),
                DateUtils.getEndDate(queryDTO.getCreateTimeArr()),
                queryDTO.getPageRequestOrderByIdDesc());

        List<SysCaptchaLogDTO> list = entityPage.stream()
                .map(e -> modelMapper.map(e, SysCaptchaLogDTO.class))
                .toList();

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());
    }

    @Override
    public void delete(Long id) {
        sysCaptchaLogRepository.deleteById(id);
    }

    @Override
    public void deleteAll() {
        sysCaptchaLogRepository.deleteAll();
    }
}
